package com.ytq.secutiry.config.filter;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Objects;

/**
 * @author yuantongqin
 * desc: 图片验证码
 * 2020-09-30
 */
@Component
public class ImageVerificationFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
        String requestURI = httpServletRequest.getRequestURI();
        if(requestURI.endsWith("/login")){
            System.out.println("登录接口来了");
            String imageCode = httpServletRequest.getParameter("imageCode");
            if(StringUtils.isEmpty(imageCode) || !Objects.equals(imageCode,"1111")){
                System.out.println("验证不通过");
                return;
            }
        }
        System.out.println("不是登录接口放行");
        filterChain.doFilter(httpServletRequest,httpServletResponse);

    }
}
